const Sequelize = require('sequelize')
const seq = require('./seq')

// 创建 users
const Users = seq.define('users', {
    username: {
        type: Sequelize.STRING, // varchar(255)
        allowNull: false
    },
    password: {
        type: Sequelize.STRING, // varchar(255)
        allowNull: false
    },
    nickname: {
        type: Sequelize.STRING, // varchar(255)
        comment: '昵称'
    }
})

// 创建 blogs
const Blogs = seq.define('blogs', {
    title: {
        type: Sequelize.STRING,
        allowNull: false
    },
    content: {
        type: Sequelize.TEXT,
        allowNull: false
    },
    userId: {
        type: Sequelize.INTEGER,
        allowNull: false
    }
})

// 外键关联
Blogs.belongsTo(Users, {
    // 创建外键
    foreignKey: 'userId'
})

Users.hasMany(Blogs, {
    // 创建外键
    foreignKey: 'userId'
})

module.exports = {
    Users,
    Blogs
}